home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Network Ne20623652001.psc / NetResource.cls < prev    next >
Encoding:
Visual Basic class definition  |  2000-01-04  |  14.9 KB  |  390 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "NetResource"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. Option Explicit
  17.  
  18. ' This is where the nasty VB is kept
  19.  
  20. Public Enum NetResourceTypes    ' Enum of possible types of NetResource
  21.     Generic = 0
  22.     Domain = 1
  23.     Server = 2
  24.     share = 3
  25.     File = 4
  26.     Group = 5
  27.     Network = 6
  28.     Root = 7
  29.     ShareAdmin = 8
  30.     Directory = 9
  31.     Tree = 10
  32.     NDSContainer = 11
  33.     Printer = &HFF
  34. End Enum
  35.  
  36. Private mvNetRes As NETRES2
  37. Private mvGotChildren As Boolean
  38. Private mvChildren As NetResources  ' Collection of child containers and disk objects (what you usually get in the Network Neighborhood tree)
  39. Private mvAmRoot As Boolean
  40. Private mvAmPrinter As Boolean
  41.  
  42. Private Declare Function GlobalAlloc Lib "KERNEL32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  43. Private Declare Function GlobalFree Lib "KERNEL32" (ByVal hMem As Long) As Long
  44. Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  45. Private Declare Function lstrcpyA Lib "KERNEL32" Alias "lstrcpy" (ByVal NewString As String, ByVal OldString As Long) As Long
  46. Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
  47. Private Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, ByVal lpBuffer As Long, ByRef lpBufferSize As Long) As Long
  48. Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
  49.  
  50. Private Type sNETRESOURCE ' API compatible NETRESOURCE structure
  51.     dwScope As Long       ' All members expressed as Long pointers
  52.     dwType As Long
  53.     dwDisplayType As Long
  54.     dwUsage As Long
  55.     lpLocalName As Long
  56.     lpRemoteName As Long
  57.     lpComment As Long
  58.     lpProvider As Long
  59. End Type
  60.    
  61. Private Type NETRES2 ' VB compatible NETRESOURCE structure
  62.     dwScope As Long  ' Members mapped back to VB datatypes
  63.     dwType As Long
  64.     dwDisplayType As Long
  65.     dwUsage As Long
  66.     lpLocalName As String
  67.     lpRemoteName As String
  68.     lpComment As String
  69.     lpProvider As String
  70. End Type
  71.  
  72. Private Const RESOURCE_CONNECTED = &H1
  73. Private Const RESOURCE_GLOBALNET = &H2
  74. Private Const RESOURCE_REMEMBERED = &H3
  75. Private Const RESOURCE_CONTEXT = &H5
  76.  
  77. Private Const RESOURCETYPE_ANY = &H0
  78. Private Const RESOURCETYPE_DISK = &H1
  79. Private Const RESOURCETYPE_PRINT = &H2
  80. Private Const RESOURCETYPE_UNKNOWN = &HFFFF
  81.  
  82. Private Const RESOURCEUSAGE_CONNECTABLE = &H1
  83. Private Const RESOURCEUSAGE_CONTAINER = &H2
  84. Private Const RESOURCEUSAGE_RESERVED = &H80000000
  85.  
  86. Private Const GMEM_DDESHARE = &H2000
  87. Private Const GMEM_DISCARDABLE = &H100
  88. Private Const GMEM_DISCARDED = &H4000
  89. Private Const GMEM_FIXED = &H0
  90. Private Const GMEM_INVALID_HANDLE = &H8000
  91. Private Const GMEM_LOCKCOUNT = &HFF
  92. Private Const GMEM_MODIFY = &H80
  93. Private Const GMEM_MOVEABLE = &H2
  94. Private Const GMEM_NOCOMPACT = &H10
  95. Private Const GMEM_NODISCARD = &H20
  96. Private Const GMEM_NOT_BANKED = &H1000
  97. Private Const GMEM_NOTIFY = &H4000
  98. Private Const GMEM_SHARE = &H2000
  99. Private Const GMEM_VALID_FLAGS = &H7F72
  100. Private Const GMEM_ZEROINIT = &H40
  101. Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
  102.  
  103. Private Const ERROR_MORE_DATA = 234
  104.  
  105. Private Const RESOURCEDISPLAYTYPE_GENERIC = 0
  106. Private Const RESOURCEDISPLAYTYPE_DOMAIN = 1
  107. Private Const RESOURCEDISPLAYTYPE_SERVER = 2
  108. Private Const RESOURCEDISPLAYTYPE_SHARE = 3
  109. Private Const RESOURCEDISPLAYTYPE_FILE = 4
  110. Private Const RESOURCEDISPLAYTYPE_GROUP = 5
  111. Private Const RESOURCEDISPLAYTYPE_NETWORK = 6
  112. Private Const RESOURCEDISPLAYTYPE_ROOT = 7
  113. Private Const RESOURCEDISPLAYTYPE_SHAREADMIN = 8
  114. Private Const RESOURCEDISPLAYTYPE_DIRECTORY = 9
  115. Private Const RESOURCEDISPLAYTYPE_TREE = &HA
  116. Private Const RESOURCEDISPLAYTYPE_NDSCONTAINER = &HB
  117.  
  118. Private Sub GetPrinters()
  119. ' API wrangling...
  120. ' Basically the same routine as GetChildren but tweaked to only return printer objects
  121. ' It also discards all non-share objects since we only want printers for this enumeration
  122.  
  123. ' Initialise my collection and variables
  124. Dim hEnum As Long, lpBuff As Long
  125. Dim cbBuff As Long, cCount As Long
  126. Dim p As Long, res As Long, i As Long
  127. Dim EnumHTemp As Long
  128. Dim reqBufferSize As Long
  129. Dim nR As sNETRESOURCE  ' API friendly structure
  130. Dim tempRes As NETRES2  ' VB friendly structure
  131. Dim tChild As NetResource
  132.  
  133. ' If this object is the Network root then we need to make a slight adjustment to the starting values
  134. ' of our API friendly NETRESOURCE structure
  135. If mvAmRoot Then
  136.     nR.dwUsage = RESOURCEUSAGE_CONNECTABLE
  137.     nR.lpRemoteName = 0
  138. End If
  139.  
  140. ' Open a net enumeration
  141. ' Limit enumeration to connectable print resources (i.e. printer objects)
  142. res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_PRINT, RESOURCEUSAGE_CONNECTABLE, mvNetRes, hEnum)
  143.  
  144. ' Check for errors
  145. If res <> 0 Then
  146.     ' Error returned when trying to open the enumeration
  147.     ' Probably means we don't have access to see its children.
  148.     ' See the MSDN for more details on possible errors.
  149.     ' Currently no trapping is done here and the routine just exits with an empty children collection
  150.     Exit Sub
  151. End If
  152.  
  153. ' Now begin to enumerate the collection
  154. EnumHTemp = hEnum
  155. ' Allocate a default buffer for the NETRESOURCE structure returned from the enum routine, say 1K
  156. cbBuff = 1024&
  157. lpBuff = GlobalAlloc(GPTR, cbBuff)
  158. Do
  159.     EnumHTemp = hEnum
  160.     cCount = &HFFFFFFFF ' Number of entries to return from enumeration - &HFFFFFFFF causes all objects to be returned
  161.     res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
  162.     If res = ERROR_MORE_DATA Then
  163.         ' The enumeration has reported that the lpBuff is not big enough to hold all of the information in the
  164.         ' NETRESOURCE structure. cbBuff has been updated to hold the required amount of space.
  165.         GlobalFree lpBuff   ' Free the memory we're using for the current small buffer
  166.         lpBuff = GlobalAlloc(GPTR, cbBuff)  ' Allocate a new space of the size requested by the enumeration
  167.     Else
  168.         If res = 0 Then     ' No error
  169.             p = lpBuff
  170.             ' cCount holds the number of NETRESOURCE structures returned in this pass
  171.             ' (The enumeration returns as many as will fit into the buffer)
  172.             For i = 1 To cCount ' Loop through the buffer, tackling each structure in turn
  173.                 CopyMemory nR, ByVal p, LenB(nR)    ' Copy the block of memory representing the structure into a local API friendly NETRESOURCE structure
  174.                 p = p + LenB(nR)    ' Step forward in the buffer by the length of the copied structure
  175.                 If nR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE Then
  176.                     tempRes.dwDisplayType = nR.dwDisplayType
  177.                     tempRes.dwScope = nR.dwScope
  178.                     tempRes.dwType = nR.dwType
  179.                     tempRes.dwUsage = nR.dwUsage
  180.                     tempRes.lpComment = lStrCpy(nR.lpComment)
  181.                     tempRes.lpLocalName = lStrCpy(nR.lpLocalName)
  182.                     tempRes.lpProvider = lStrCpy(nR.lpProvider)
  183.                     tempRes.lpRemoteName = lStrCpy(nR.lpRemoteName)
  184.                     Set tChild = New NetResource
  185.                     tChild.NRStruct = tempRes
  186.                     tChild.IsPrinter = True ' I know this is a bit of a fudge, but I didn't think it worth the effort to write polymorphic classes for such a small matter
  187.                     mvChildren.Add tChild
  188.                 End If
  189.             Next
  190.         End If
  191.     End If
  192. Loop Until cCount = 0
  193. ' Close the enum
  194. WNetCloseEnum hEnum
  195. ' Free the memory
  196. GlobalFree lpBuff
  197.  
  198. End Sub
  199.  
  200. Friend Property Let IsPrinter(pVal As Boolean)
  201. mvAmPrinter = pVal
  202. End Property
  203.  
  204. Private Function lStrCpy(lStrPointer As Long) As String
  205. Dim TString As String
  206. TString = String(255, Chr$(0))
  207. lstrcpyA TString, lStrPointer
  208. lStrCpy = Left(TString, InStr(TString, Chr$(0)) - 1)
  209. End Function
  210.  
  211. Public Property Get Children() As NetResources
  212. If Not mvGotChildren Then GetChildren
  213. Set Children = mvChildren
  214. End Property
  215.  
  216.  
  217.  
  218. Public Property Get Comment() As String
  219. Comment = mvNetRes.lpComment
  220. End Property
  221.  
  222. Private Sub GetChildren()
  223. ' API wrangling...
  224.  
  225. ' Initialise my collection and variables
  226. Set mvChildren = New NetResources
  227. Dim hEnum As Long, lpBuff As Long
  228. Dim cbBuff As Long, cCount As Long
  229. Dim p As Long, res As Long, i As Long
  230. Dim EnumHTemp As Long
  231. Dim reqBufferSize As Long
  232. Dim nR As sNETRESOURCE  ' API friendly structure
  233. Dim tempRes As NETRES2  ' VB friendly structure
  234. Dim tChild As NetResource
  235.  
  236. ' If this object is the Network root then we need to make a slight adjustment to the starting values
  237. ' of our API friendly NETRESOURCE structure
  238. If mvAmRoot Then
  239.     nR.dwUsage = RESOURCEUSAGE_CONNECTABLE
  240.     nR.lpRemoteName = 0
  241. End If
  242.  
  243. ' Open a net enumeration
  244. res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, mvNetRes, hEnum)
  245.  
  246. ' Check for errors
  247. If res <> 0 Then
  248.     ' Error returned when trying to open the enumeration
  249.     ' Probably means we don't have access to see its children.
  250.     ' See the MSDN for more details on possible errors.
  251.     ' Currently no trapping is done here and the routine just exits with an empty children collection
  252.     Exit Sub
  253. End If
  254.  
  255. ' Now begin to enumerate the collection
  256. EnumHTemp = hEnum
  257. ' Allocate a default buffer for the NETRESOURCE structure returned from the enum routine, say 1K
  258. cbBuff = 1024&
  259. lpBuff = GlobalAlloc(GPTR, cbBuff)
  260. Do
  261.     EnumHTemp = hEnum
  262.     cCount = &HFFFFFFFF ' Number of entries to return from enumeration - &HFFFFFFFF causes all objects to be returned
  263.     res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
  264.     If res = ERROR_MORE_DATA Then
  265.         ' The enumeration has reported that the lpBuff is not big enough to hold all of the information in the
  266.         ' NETRESOURCE structure. cbBuff has been updated to hold the required amount of space.
  267.         GlobalFree lpBuff   ' Free the memory we're using for the current small buffer
  268.         lpBuff = GlobalAlloc(GPTR, cbBuff)  ' Allocate a new space of the size requested by the enumeration
  269.     Else
  270.         If res = 0 Then     ' No error
  271.             p = lpBuff
  272.             ' cCount holds the number of NETRESOURCE structures returned in this pass
  273.             ' (The enumeration returns as many as will fit into the buffer)
  274.             For i = 1 To cCount ' Loop through the buffer, tackling each structure in turn
  275.                 CopyMemory nR, ByVal p, LenB(nR)    ' Copy the block of memory representing the structure into a local API friendly NETRESOURCE structure
  276.                 p = p + LenB(nR)    ' Step forward in the buffer by the length of the copied structure
  277.                 tempRes.dwDisplayType = nR.dwDisplayType    ' Begin copying the members of the API friendly structure to the VB friendly structure
  278.                 tempRes.dwScope = nR.dwScope
  279.                 tempRes.dwType = nR.dwType
  280.                 tempRes.dwUsage = nR.dwUsage
  281.                 tempRes.lpComment = lStrCpy(nR.lpComment)   ' String copies accomplished by using the lStrCpy routine
  282.                 tempRes.lpLocalName = lStrCpy(nR.lpLocalName)
  283.                 tempRes.lpProvider = lStrCpy(nR.lpProvider)
  284.                 tempRes.lpRemoteName = lStrCpy(nR.lpRemoteName)
  285.                 Set tChild = New NetResource    ' Create the new NetResource object that will be the new child
  286.                 tChild.NRStruct = tempRes   ' Pass the current VB friendly NETRESOURCE structure to tbe force populate method of the NetResource object
  287.                 mvChildren.Add tChild   ' Add the new object to my children collection
  288.             Next
  289.         End If
  290.     End If
  291. Loop Until cCount = 0
  292. ' Close the enum
  293. WNetCloseEnum hEnum
  294. ' Free the memory
  295. GlobalFree lpBuff
  296.  
  297. ' In order to distinguish printers from other shares we need to enumerate them separately
  298. GetPrinters
  299.  
  300. mvGotChildren = True
  301.  
  302. End Sub
  303.  
  304. Public Property Get LocalName() As String
  305. LocalName = mvNetRes.lpLocalName
  306.  
  307. End Property
  308.  
  309.  
  310. Friend Property Let NRStruct(RHS As NETRES2)
  311. ' Private force populate routine
  312. ' When a NetResource object it defaults to being the network root object
  313. ' The only way to change this is to call this routine and pass a VB friendly NETRES2 NETRESOURCE structure
  314. ' When this function is called correctly it populates the data for this NetResource and forces it to act as a child rather than
  315. ' a network root.
  316. ' When compiled as a COM DLL this function will not be visible to the user - it's intended for internal use only
  317. mvNetRes = RHS
  318. mvAmRoot = False
  319. End Property
  320.  
  321.  
  322.  
  323. Public Property Get Provider() As String
  324. Provider = mvNetRes.lpProvider
  325. End Property
  326.  
  327. Public Property Get RemoteName() As String
  328. RemoteName = mvNetRes.lpRemoteName
  329. End Property
  330.  
  331.  
  332. Public Property Get ResourceType() As NetResourceTypes
  333. If Not mvAmPrinter Then ResourceType = mvNetRes.dwDisplayType Else ResourceType = Printer
  334.  
  335. End Property
  336.  
  337. Public Property Get ResourceTypeName() As String
  338. ' Provides a friendly name for the resource type as an alternative to using the enumerated "ResourceType" property
  339. ' This can be used to quicky bind NetResource objects to named images in an imagelist control (for example)
  340. If mvAmPrinter Then
  341.     ResourceTypeName = "Printer"
  342.     Exit Property
  343. End If
  344. Select Case mvNetRes.dwDisplayType
  345.     Case RESOURCEDISPLAYTYPE_GENERIC
  346.         ResourceTypeName = "Generic"
  347.     Case RESOURCEDISPLAYTYPE_DOMAIN
  348.         ResourceTypeName = "Domain"
  349.     Case RESOURCEDISPLAYTYPE_SERVER
  350.         ResourceTypeName = "Server"
  351.     Case RESOURCEDISPLAYTYPE_SHARE
  352.         ResourceTypeName = "Share"
  353.     Case RESOURCEDISPLAYTYPE_FILE
  354.         ResourceTypeName = "File"
  355.     Case RESOURCEDISPLAYTYPE_GROUP
  356.         ResourceTypeName = "Group"
  357.     Case RESOURCEDISPLAYTYPE_NETWORK
  358.         ResourceTypeName = "Network"
  359.     Case RESOURCEDISPLAYTYPE_ROOT
  360.         ResourceTypeName = "Root"
  361.     Case RESOURCEDISPLAYTYPE_SHAREADMIN
  362.         ResourceTypeName = "AdminShare"
  363.     Case RESOURCEDISPLAYTYPE_DIRECTORY
  364.         ResourceTypeName = "Directory"
  365.     Case RESOURCEDISPLAYTYPE_TREE
  366.         ResourceTypeName = "Tree"
  367.     Case RESOURCEDISPLAYTYPE_NDSCONTAINER
  368.         ResourceTypeName = "NDSContainer"
  369. End Select
  370. End Property
  371.  
  372. Public Property Get ShortName() As String
  373. ' Return just the final part of the object's name (rather than a fully qualified path or context)
  374. Dim i As Integer
  375. i = InStrRev(mvNetRes.lpRemoteName, "\")
  376. ShortName = Right(mvNetRes.lpRemoteName, Len(mvNetRes.lpRemoteName) - i)
  377. End Property
  378.  
  379.  
  380. Private Sub Class_Initialize()
  381. mvAmRoot = True
  382. End Sub
  383.  
  384.  
  385. Private Sub Class_Terminate()
  386. Set mvChildren = Nothing
  387. End Sub
  388.  
  389.  
  390.